<?php

namespace App\Http\Controllers;

use App\loan_customer;
use Illuminate\Http\Request;


class LoansController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {


        return view('loans.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $id = $request->input('id');

        if(in_array($id, [1, 2, 3])){
            return view('loans.basic', compact('id'));
        } else {
            return view('loans.help');
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        $id = $request->input('id');
        // TODO:

        $file = $request->input('file_id', []);
        $limits=$request->input('year')*12;
        if (count($file)) {
            $file = json_encode($file);
        }
        if($request->input('id')==1){
            if($request->input('year')==1){
                $repayments=($request->input('money')*(1+0.0435))/12;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==3){
                $repayments=($request->input('money')*(1+0.0475))/36;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==5){
                $repayments=($request->input('money')*(1+0.05))/60;
                $repayment=round($repayments,2);
            }
        }elseif($request->input('id')==2){
            if($request->input('year')==1){
                $repayments=($request->input('money')*(1+0.0445))/12;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==3){
                $repayments=($request->input('money')*(1+0.0485))/36;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==5){
                $repayments=($request->input('money')*(1+0.051))/60;
                $repayment=round($repayments,2);
            }
        }else{
            if($request->input('year')==1){
                $repayments=($request->input('money')*(1+0.0455))/12;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==3){
                $repayments=($request->input('money')*(1+0.0495))/36;
                $repayment=round($repayments,2);
            }
            if($request->input('year')==5){
                $repayments=($request->input('money')*(1+0.052))/60;
                $repayment=round($repayments,2);
            }
        }
        $data = $request->except('_token');
        $status = loan_customer::create([
            'money_type' => $data['id'],
            'name' => $data['name'],
            'Id_per' => $data['id_card'],
            'email' => $data['email'],
            'tel' => $data['tel'],
            'addr' => $data['addr'],
            'money' => $data['money'],
            'img' =>$file,
            'data_type' => $data['year'],
            'limits'=>$limits,
            'repayment'=>$repayment

        ]);
        if ($status) {
            return redirect('/loans')->with('success', '贷款申请资料提交成功');
        } else {
            return redirect('/loans')->with('danger', '贷款申资料提交请失败');
        }




    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id )
    {
//        dd($id);


    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');

        $file = upload_file($file, 'loans/');

        if ($file['status']) {
            return [
                'code' => 1000,
                'path' => $file['file_path'],
                'id' => $file['name']
            ];
        } else {
            return [
                'code' => 1001,
                'msg' => $file['msg']
            ];
        }
    }

    public function removeFile($id)
    {
        $id = explode('-', $id);

        $file = 'Uploads/loans/' . date('Y-m-d') . '/' . $id[0] . '.' . $id[1];

        if (file_exists($file)) {
            @unlink($file);
        }

        return [
            'code' => 1000,
        ];
    }
}
